from django.shortcuts import render
import qrcode
import io
from PIL import Image
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
# Create your views here.

# 生成邀请码
class CodeView(APIView):
    def get(self, request):
        username = request.GET.get('username')
        user_obj = CodeModel.objects.filter(username=username).first()
        qr = qrcode.QRCode(
            version=4,  # 生成二维码尺寸的大小 1-40 1:21*21（21+(n-1)*4）
            error_correction=qrcode.constants.ERROR_CORRECT_M,  # L:7% M:15% Q:25% H:30%
            box_size=10,  # 每个格子的像素大小
            border=2,  # 边框的格子宽度大小
        )
        # save_path = 'theqrcode.png'  # 生成后的保存文件
        # logo = 'logo.jpg'  # logo图片
        qr.add_data(username)
        qr.make(fit=True)
        img = qr.make_image()
        buf = io.BytesIO()
        img.save(buf, 'png')
        return Response({'msg': 'OK'})